//
//  WLMWelcomeViewController.swift
//  WeiBo
//
//  Created by Eric on 2017-01-08.
//  Copyright © 2017 Eric. All rights reserved.
//

import UIKit

private let iconSize: CGFloat = 90
private let bottomMargin: CGFloat = 160

class WLMWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        setupUI()
    }
    
    
    //在视图已经显示的阶段执行动画效果
    //自动布局的界面如果在ViewDidLoad执行动画效果,子控件的frame的origin.x,origin.y都是 0零
    //将动画效果放在viewDidAppear阶段执行
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    private func setupUI() {
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        //SnapKit
        iconView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-bottomMargin)
            make.size.equalTo(CGSize(width: iconSize, height: iconSize))
        }
        
        welcomeLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.view)
            make.top.equalTo(iconView.snp.bottom).offset(20)
        }
        
        //切圆角
        iconView.layer.cornerRadius = iconSize / 2
        iconView.layer.masksToBounds = iconView.layer.cornerRadius > 0
        
        //设置网络头像
        iconView.yy_setImage(with: WLMUserAccountViewModel.shared.avatarURL, placeholder: #imageLiteral(resourceName: "avatar_default_big"))
        
    }
    
    //动画效果
    private func startAnimation() {
        let offset = -(ScreenHeight - iconSize - bottomMargin)
        
        self.iconView.snp.updateConstraints({ (make) in
            make.bottom.equalTo(self.view).offset(offset)
        })
        
        UIView.animate(withDuration: 1.2, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: {
            //强制跟新约束
            self.view.layoutIfNeeded()
        }, completion: { (_) -> () in
            UIView.animate(withDuration: 0.5, animations: {
                self.welcomeLabel.alpha = 1
            }, completion: { (_) in
                //执行页面的跳转
                let transition = CATransition()
                transition.type = "rippleEffect"
                transition.duration = 1.5
                self.view.layer.add(transition, forKey: nil)
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.6, execute: {
                    NotificationCenter.default.post(name: NSNotification.Name.AppChangeRootVC, object: "WLMMainTabBarController")
                })
            })
        })
    }
    
    //用户头像
    lazy var iconView: UIImageView = UIImageView(image: #imageLiteral(resourceName: "avatar_default"))
    //欢迎文字
    lazy var welcomeLabel: UILabel = {
        let l = UILabel(text: "\(WLMUserAccountViewModel.shared.account?.screen_name ?? "")欢迎归来", fonSize: 15, textColor: UIColor.darkGray)
        l.alpha = 0
        return l
    }()
}
